package com.example.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crm.entity.Right;
import com.example.crm.entity.Role;
import com.example.crm.entity.RoleRight;
import com.example.crm.entity.User;
import com.example.crm.service.IRightService;
import com.example.crm.service.IRoleRightService;
import com.example.crm.service.IRoleService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zz
 * @since 2024-06-06
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Resource
    IRoleService roleService;

    @Resource
    IRoleRightService roleRightService;

    @Resource
    IRightService rightService;
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(defaultValue = "1")Integer pageIndex,
                       @RequestParam(value = "roleName",required = false)String roleName){
        LambdaQueryWrapper<Role> queryWrapper = null;
        if(roleName != null && !"".equals(roleName.trim())){
            queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.like(Role::getRoleName,roleName);
            queryWrapper.eq(Role::getRoleFlag,1);
        }
        Page<Role> page=new Page<>(pageIndex,3);
        Page<Role> rolePage =roleService.page(page,queryWrapper);
        model.addAttribute("rolePage",rolePage);
        model.addAttribute("roleName",roleName);
        return "role/list";
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam("roleId")String roleId,Model model){
        Role role = roleService.getById(roleId);
        List<Right> rightList=rightService.selectByRoleId(Integer.valueOf(roleId));
        List<Right> rights=rightService.list();
        model.addAttribute("rights",rights);
        model.addAttribute("role",role);
        model.addAttribute("rightList",rightList);
        return "role/edit";
    }
    @RequestMapping("/modify")
    public String editComplete(Role role,@RequestParam("rightCodes") List<String> rightCodes){
        LambdaQueryWrapper<RoleRight> sysRoleRightLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleRightLambdaQueryWrapper.eq(RoleRight::getRfRoleId,role.getRoleId());
        roleRightService.remove(sysRoleRightLambdaQueryWrapper);
        RoleRight sysRoleRight = null;
        if (roleService.updateById(role)) {
            for (String rightCode : rightCodes) {
                sysRoleRight = new RoleRight();
                sysRoleRight.setRfRightCode(rightCode);
                sysRoleRight.setRfRoleId(role.getRoleId());
                roleRightService.save(sysRoleRight);
            }
            return "redirect:/role/list";
        }else {
            return "redirect:/role/edit";
        }
    }
    @DeleteMapping("/del")
    @ResponseBody
    public Object del(@RequestParam("roleId") Long roleId){
        Map map=new HashMap();
        try{
            if(roleService.del(roleId)){
                map.put("delResult", "true");
            }else {
                map.put("delResult", "false");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping("/add")
    public String add(Model model) {
        List<Right> rights = rightService.list();
        model.addAttribute(rights);
        return "role/add";
    }

    @PostMapping("/save")
    public String add(Role role,@RequestParam("rightCodes") List<String> rightCodes) {
        roleService.save(role);
        RoleRight sysRoleRight = null;
        for (String rightCode : rightCodes) {
            sysRoleRight = new RoleRight();
            sysRoleRight.setRfRightCode(rightCode);
            sysRoleRight.setRfRoleId(role.getRoleId());
            roleRightService.save(sysRoleRight);
        }
        return "redirect:/role/list";
    }
}
